var addOrEditDialog,versionDialog;
    require(['date','validator','grid'], function () {
	var $stnDuraSetList = $("#stnDuraSetList"),
		$addSet = $("#addSet"),
        $selectSet = $("#selectSet"),
        $editSet = $("#editSet"),
		$delSet = $("#delSet"),
		$saveSet = $("#saveSet"),$formData=$("#formData"),
		selLineId,selLineName,lastsel2,corpId,nodes,
	    vrsnName="",lineType="",strtusDate="",dateType="",beginTime="",endTime="",beginStnCd="",beginStnSn="",
	    endStnCd="",endStnSn="",minStnDura="",maxStnDura="",vrsnCd="",vrsnCdSel="",strtusDateArr="",vrsionName="",versionCd;
	//grid初始化
    function gridInit() {
    	$stnDuraSetList.grid({
			url :"/operationReport/searchStnDura.htm",
			colModel:[
				{name: "id", label: "id", key: true, hidden: true},
				{name: "vrsnName", label: "站点区间",align: "left"},
				{name: "strtusDate", label: "启用日期",align: "center"},
				{name: "lineType", label: "线路类别",dict: "LIT00000",align: "left"},
				{name: "beginStnSn", label: "开始站点序号",align: "right"},
				{name: "beginStnName", label: "开始站点名称",align: "left"},
				{name: "endStnSn", label: "结束站点序号",align: "right"},
				{name: "endStnName", label: "结束站点名称",align: "left"},
				{name: "minStnDura", label: "最小站间时长(S)",align: "right",editable:false,  edittype:'text',editoptions:{size:6,maxlength:5},editrules:{required:true}},
				{name: "maxStnDura", label: "最大站间时长(S)",align: "right",editable:false, edittype:'text',editoptions:{size:6,maxlength:5}, editrules:{required:true}},
				{name: "vrsnCd", label: "版本编号",hidden: true},
				{name: "beginStnCd", label: "站点编号",hidden: true},
				{name: "lineType", label: "lineTypeStatus",hidden: true},
				{name: "beginStnCd", label: "beginStnCd",hidden: true},
				{name: "endStnCd", label: "endStnCd",hidden: true}
			],
			onSelectRow: function(id){
				if(id && id!==lastsel2){
					jQuery('#stnDuraSetList').grid('saveRow',lastsel2);
					jQuery('#stnDuraSetList').grid('editRow',id,false);
					lastsel2=id;
				}
			},
			multiselect: true,
			rowNum:1000,
			height:$(window).height()-218,
			showPager: false
		});
    }

	//左侧树
	commonFun.leftTreeNodeClickObj['stnDuraSetTab'] = function (event, treeId, treeNode) {
		if(treeNode.type==='LINE'){
			selLineId=treeNode.id;
			corpId=treeNode.corpId;
			selLineName=treeNode.name;
			getStnDuraSel();
            $("#stnDuraVrsnSel").val(null);
		}else{
			$.messager.alert("提示","请选择一条线路进行查看。");
		}
	};

    //弹窗
    function getDialog(option) {
        option = option || {};
        if (versionDialog) {
            versionDialog.dialog('open');
            if (option.title) {
                versionDialog.dialog('setOption', 'title', option.title);
            }
            return versionDialog;
        } else {
            versionDialog = $('<div>').dialog({
                title: option.title || '新增/编辑站点区间',
                url: './service/setting/funStationTimeDetailSet.htm',
                height: 482,
                width: 900,
                autoOpen: true ,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                            var _t = this;
                            $(_t).dialog('exec', "save",function(){
                                debugger
                                $(_t).dialog('close');
								getStnDuraSel();

                            });
                        }
                    },{
                        text: '取消',
                        css: 'btn inline-block ml5',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }]
            })
            return versionDialog;

        }
    }
   //查询
    $selectSet.click(function() {
        $("#stnDuraSetList").jqGrid('setGridParam', {page:1,datatype: "json",postData:{"lineId":selLineId,"vrsnName":$("#stnDuraVrsnSel").val()}}).trigger("reloadGrid");
    });
    //新增
    $addSet.click(function() {
        if(selLineId==undefined){
            $.messager.alert("提示","请选择一条路线。");
            return;
        }
        addOrEditDialog= getDialog()
        addOrEditDialog.dialog('open');
        addOrEditDialog.dialog('exec', 'openDialog',selLineId,"",corpId,selLineName);
    });
    //编辑
    $editSet.click(function() {
        if(selLineId==undefined){
            $.messager.alert("提示","请选择一条路线。");
            return;
        }
		var rowData=$("#stnDuraSetList").jqGrid('getSelections');
		if(rowData==null||rowData.length!=1){
			$.messager.alert("提示","请选择一条记录。");
			return;
		}
        addOrEditDialog= getDialog()
        addOrEditDialog.dialog('open');
        addOrEditDialog.dialog('exec', 'openDialog',selLineId,rowData[0].vrsnCd,corpId,selLineName);
    });
	$('#stnDuraSetList').on('keyup',':text', function () {
	        $(this).val($(this).val().replace(/[^0-9]/g, ''));
	});


	// //根据版本查询线路信息
	// $("#stnDuraVrsnSel").on('change',function(){
	// 	$.ajaxExt({
	// 		url : "/operationReport/getStnDuraVrsnName.htm",
	// 		data : {"lineId":selLineId,"vrsnName":$("#stnDuraVrsnSel").val()}
	// 	}).done(function(data){
	// 		$("#beginTimeStnDuraSet").val(data[0].beginTime);
	// 		$("#endTimeStnDuraSet").val(data[0].endTime);
	// 		$('input[name="stnDuraParameSet"]').prop('checked',false);
	// 		$("#stnDura"+data[0].dateType).prop("checked",true);
	// 		$("#stnDuraLineType").val(data[0].lineType);
	// 		$("#stnDuraBeginStn option").eq(data[0].beginStnSn).attr("selected",true);
	// 		$("#stnDuraEndStn option").eq(data[0].endStnSn).attr("selected",true);
	// 		$("#minStnDura").val(data[0].minStnDura);
	// 		$("#maxStnDura").val(data[0].maxStnDura);
	// 		$("#stnDuraVrsnName").val(data[0].vrsnName);
	// 		$("#stnDuraStrtusDate").val(data[0].strtusDate);
	// 	});
	// });

	//删除版本
	$delSet.click(function(){
		var rowData=$("#stnDuraSetList").jqGrid('getSelections');
		if(rowData==null){
			$.messager.alert("提示","请选择一条记录。");
			return;
		}
		var ids="";
		for(var i=0;i<rowData.length;i++){
			ids+=rowData[i].vrsnCd+","
		}
		$.messager.confirm("是否删除","是否删除该信息?",function(r){
			if(r){
				$.ajaxExt({
					url:"/operationReport/delStnDura.htm",
					data : {"vrsnCd":ids}
				}).done(function(){
					getStnDuraSel();
				});
			}
		});
	});

	//根据线路类别填充站点
	$("#stnDuraLineType").on('change',function(e,fn){
		nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes.length==0){
			$.messager.alert("提示","请选择一条线路查看");
			return;
		}
		var stnDuraLineType = $("#stnDuraLineType").val();
		if(stnDuraLineType=="LIT00003"){
			stnDuraLineType=="LIT00001";
		}
		$.ajaxExt({
			url : "/operationReport/searchStationBehaviorPage.htm",
			data : {"lineId":selLineId,"lineType":stnDuraLineType,"stnDuraForAddOrEdit":"add"}
		}).done(function(data){
			var beginStnHtml="";
			$(data).each(function(i,item){
				beginStnHtml+='<option path="'+item.stnCd+'" value="'+item.stnSn+'"'+(i===0?"selected":"")+'>'+item.stnName+'</option>'
			});
			$("#stnDuraBeginStn").html(beginStnHtml);
			$.ajaxExt({
				url : "/operationReport/searchStationBehaviorPage.htm",
				data : {"lineId":selLineId,"lineType":stnDuraLineType,"stnDuraForAddOrEdit":"add"}
			}).done(function(data){
				var endStnHtml="";
				$(data).each(function(i,item){
					endStnHtml+='<option path="'+item.stnCd+'" value="'+item.stnSn+'"'+(i===0?"selected":"")+'>'+item.stnName+'</option>'
				});
				$("#stnDuraEndStn").html(endStnHtml);
				if(fn){
					fn();
				}
			});
		});
	});
	
	//根据线路查数据
	function getStnDuraSel(){
		$("#stnDuraSetList").jqGrid('setGridParam', {page:1,datatype: "json",postData:{"lineId":selLineId,"vrsnName":""}}).trigger("reloadGrid");
	}

	//初始化grid
    gridInit();
		$(document.body).keydown(function () {
			if (event.keyCode == 13) { //回车键的键值为13
				debugger
				$selectSet.trigger('click');
			}
		});
	});
